﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using TravelAgencyDAL.DAL;
using SearchOperations.DAL;
using SearchOperations;
using System.Collections.Generic;

namespace ClientSide
{
    public partial class frmReservations : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddStartPoint.DataSource = BookOperations.GetAllSettlements(true);
                ddStartPoint.DataTextField = "SettlementName";
                ddStartPoint.DataValueField = "SettlementID";
                ddStartPoint.AutoPostBack = true;
                ddStartPoint.DataBind();
                panelDetails.Visible = false;
                lblNote.Visible = false;
                panelPass.Visible = false;
            }
            if (Session["type"]!=null && Session["type"].ToString()=="book")
            {
                ddStartPoint.SelectedIndex=(int)(Session["startI"]);
                GetConnectedSettlement(Session["start"].ToString());
                ddEndPoint.SelectedIndex=(int)(Session["endI"]);
                tbDate.Text=Session["date"].ToString();
                TravelDetailsBound(Session["start"].ToString(), Session["end"].ToString(), Session["date"].ToString());
            }

            ddStartPoint.SelectedIndexChanged += new EventHandler(ddStartPoint_SelectedIndexChanged);
            ddEndPoint.SelectedIndexChanged += new EventHandler(ddEndPoint_SelectedIndexChanged);
            btnShow.Click += new EventHandler(btnShow_Click);
            btnConfirm.Click += new EventHandler(btnConfirm_Click);
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            Session["type"] = null;
            string details = Session["book"].ToString() + ";" + tbPassengers.Text;
            string str=BookOperations.Book(details,User.Identity.Name);

            lblBookNote.Text = str;
            panelDetails.Visible = false;
            lblNote.Visible = false;
            lblBookNote.Visible = true;
        }

        protected void ddEndPoint_SelectedIndexChanged(object sender, EventArgs e)
        {
            panelDetails.Visible = false;
            lblNote.Visible = false;
            gvTravelDetails.Visible = false;
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            gvTravelDetails.Visible = true;
            panelPass.Visible = false;
            panelDetails.Visible = false;
            lblNote.Visible = false;
            lblBookNote.Visible = false;

            if (ddStartPoint.SelectedValue != string.Empty & ddEndPoint.SelectedValue != string.Empty)
            {
                lblRequired.Text = string.Empty;

                TravelDetailsBound(ddStartPoint.SelectedValue, ddEndPoint.SelectedValue, tbDate.Text);
            }
            else
            {
                lblRequired.Text = "Settlement required!";
            }
        }

        private void TravelDetailsBound(string start, string end, string date)
        {
            gvTravelDetails.Visible = true;
            string[] dateArr = date.Split('/');
            DateTime tDate = new DateTime(int.Parse(dateArr[2]),
                int.Parse(dateArr[0]), int.Parse(dateArr[1]));

            gvTravelDetails.DataSource = BookOperations.GetRoutes
                (long.Parse(start), long.Parse(end),
                tDate);
            gvTravelDetails.DataBind();
        }

        protected void ddStartPoint_SelectedIndexChanged(object sender, EventArgs e)
        {
            panelDetails.Visible = false;
            lblNote.Visible = false;
            gvTravelDetails.Visible = false;
            GetConnectedSettlement(ddStartPoint.SelectedValue);
        }

        private void GetConnectedSettlement(string start)
        {
            ddEndPoint.DataSource = BookOperations.GetAllConnectedSettlements
                (long.Parse(start));
            ddEndPoint.DataTextField = "SettlementName";
            ddEndPoint.DataValueField = "SettlementID";
            ddEndPoint.DataBind();
        }

        protected void Row_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Book")
            {
                if (User.Identity.Name == "")
                {
                    Session["type"] = "book";
                    Session["start"] = ddStartPoint.SelectedValue;
                    Session["startI"] = ddStartPoint.SelectedIndex;
                    Session["end"] = ddEndPoint.SelectedValue;
                    Session["endI"] = ddEndPoint.SelectedIndex;
                    Session["date"] = tbDate.Text;
                    Response.Redirect("frmLogin1.aspx");
                }
                else
                {
                    lblNote.Visible = false;
                    lblBookNote.Visible = false;
                    panelDetails.Visible = false;
                    panelPass.Visible = true;
                    tbPassengers.Text = string.Empty;
                    GridViewRow row = gvTravelDetails.Rows[int.Parse(e.CommandArgument.ToString())];
                    Session["book"] = row.Cells[0].Text + ";" + row.Cells[1].Text + ";" + ddStartPoint.SelectedValue +
                        ";" + ddEndPoint.SelectedValue + ";" + tbDate.Text;
                }
            }
            else if (e.CommandName == "Cancel")
            {
                Session["type"] = null;
                panelDetails.Visible = false;
                panelPass.Visible = false;
                lblBookNote.Visible = false;
                lblNote.Visible = false;
                gvTravelDetails.Visible= false;
            }
            else
            {
                DeatailsShow(gvTravelDetails.Rows[int.Parse(e.CommandArgument.ToString())].Cells[0].Text,
                    ddStartPoint.SelectedValue, ddEndPoint.SelectedValue);
                lblDestName.Text = "Detination: " +
                gvTravelDetails.Rows[int.Parse(e.CommandArgument.ToString())].Cells[0].Text;
                panelDetails.Visible = true;
                lblNote.Visible = true;
                lblBookNote.Visible = false;
                panelPass.Visible = false;
            }
        }

        private void DeatailsShow(string destinationName, string startPointID, string endPointID)
        {
            List<RouteDetails> rdList = BookOperations.GetRouteDetails(destinationName, startPointID, endPointID);

            repDetails.DataSource = rdList;
            repDetails.DataBind();
        }

        protected void Row_Canceling(object sender, EventArgs e)
        {
            Session["type"] = null;
            panelDetails.Visible = false;
            panelPass.Visible = false;
            lblBookNote.Visible = false;
            lblNote.Visible = false;
            gvTravelDetails.Visible = false;
        }
    }
}
